﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RecursosHumanos
{
    public partial class frmBIWizard : Form
    {
        private short TabAtual = 1, TabAnt = 0;
        private int FonteAnt;
        private List<BIModeloCampo> lstCampoSel = new List<BIModeloCampo>();
        private List<BIModeloCampo> lstCampoMod = new List<BIModeloCampo>();

        public frmBIWizard()
        {
            InitializeComponent();
        }

        private void frmPessoa_Load(object sender, EventArgs e)
        {
            //(tabCampos as Control).Enabled = false;
            //(tabPreview as Control).Enabled = false;
            //(tabTitulo as Control).Enabled = false;
            FormataTela();
        }

        private void FormataTela()
        {
            switch (TabAtual)
            {
                case 1:
                    btnVoltar.Enabled = false;
                    btnProximo.Enabled = true;
                    btnFinalizar.Enabled = false;
                    txtObservacao.Text = "";
                    break;
                case 2:
                    btnVoltar.Enabled = false;
                    btnProximo.Enabled = true;
                    btnFinalizar.Enabled = false;
                    txtObservacao.Text = "Essa etapa não poderá ser alterada.";
                    //(tabCampos as Control).Enabled = true;
                    CarregaFontes();
                    break;
                case 3:
                    btnVoltar.Enabled = false;
                    btnProximo.Enabled = true;
                    btnFinalizar.Enabled = false;
                    txtObservacao.Text = "";
                    //(tabFonte as Control).Enabled = false;
                    //(tabPreview as Control).Enabled = true;
                    CarregaCampos();
                    break;
                case 4:
                    btnVoltar.Enabled = true;
                    btnProximo.Enabled = true;
                    btnFinalizar.Enabled = false;
                    txtObservacao.Text = "";
                    //(tabTitulo as Control).Enabled = true;
                    break;
                case 5:
                    btnVoltar.Enabled = true;
                    btnProximo.Enabled = false;
                    btnFinalizar.Enabled = true;
                    txtObservacao.Text = "Clique em \"Finalizar\" para concluir a criação do BI.";
                    break;
            }
            TabAnt = TabAtual;
        }

        private void CarregaCampos()
        {
            int id = ((BIModeloTabela)cbFontes.SelectedItem).idTabela;
            if (id != FonteAnt)
            {
                FonteAnt = id;
                grdCampos.Rows.Clear();
                var bd = new WBGEntities();
                lstCampoMod.Clear();
                lstCampoMod.AddRange(bd.BIModeloCampo.Where(x => x.idTabela == id));
                grdModelo.DataSource = lstCampoMod;
            }
        }

        private void CarregaFontes()
        {
            var bd = new WBGEntities();
            cbFontes.DataSource = bd.BIModeloTabela;
            cbFontes.DisplayMember = "rotulo";
            cbFontes.ValueMember = "idTabela";
        }

        private void frmLocacao__KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void btnFinalizar_Click(object sender, EventArgs e)
        {
            if (txtNome.Text == "")
            {
                MessageBox.Show("Por favor, nomeie o seu BI.", "Atenção",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                return;
            }
        }

        private void btnProximo_Click(object sender, EventArgs e)
        {
            //tabControl1.TabPages[TabAtual].IsAccessible = false;
            TabAtual++;
            //tabControl1.TabPages[TabAtual].IsAccessible = true;
            tabControl1.TabPages[TabAtual].Select();
            FormataTela();
        }

        private void btnVoltar_Click(object sender, EventArgs e)
        {
            //tabControl1.TabPages[TabAtual].IsAccessible = false;
            TabAtual--; 
            //tabControl1.TabPages[TabAtual].IsAccessible = true;
            tabControl1.TabPages[TabAtual].Focus();
            FormataTela();
        }

        private void grd_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int campo = Convert.ToInt32(grdModelo.CurrentRow.Cells["idCampo"].Value);
            if (lstCampoSel.FirstOrDefault(x => x.idCampo == campo) != null)
                return;
            lstCampoSel.Add(new BIModeloCampo()
            {
                idCampo = campo,
                rotulo = grdModelo.CurrentRow.Cells["rotulo"].Value.ToString()
            }
            );
            grdCampos.DataSource = lstCampoSel;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            lstCampoSel.Clear();
            lstCampoSel.AddRange(lstCampoMod);
            //foreach (var item in grdModelo.DataSource as List<BIModeloCampo>)
            //{
            //    lstCampoSel.Add(new BIModeloCampo()
            //    {
            //        idCampo = Convert.ToInt32(grdModelo.CurrentRow.Cells["idCampo"].Value),
            //        rotulo = grdModelo.CurrentRow.Cells["rotulo"].Value.ToString()
            //    }
            //    );
            //}
            grdCampos.DataSource = lstCampoSel;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            lstCampoSel.Clear();
            grdCampos.DataSource = lstCampoSel;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //grdCampos.Rows.Remove(grdCampos.CurrentRow);
            int campo = Convert.ToInt32(grdModelo.CurrentRow.Cells["idCampo"].Value);
            lstCampoSel.Remove(lstCampoSel.FirstOrDefault(x => x.idCampo == campo));
            grdCampos.DataSource = lstCampoSel;
        }
    }
}
